//
//  SEChatViewController+Subviews.swift
//  SmartEyeForPatient
//
//  Created by QDSG on 2021/4/8.
//

import UIKit

private let kMoreKeyboardHeight: CGFloat = 100 + UIScreen.bottomSafeHeight

// MARK: - subviews 初始化

extension SEChatViewController {
    /// 设置 chatVc 中各个 subviews
    func setupSubviews() {
        setupCountdownTimeView()
        setupActionBar()
        setupMsgTableView()
        setupChatMoreView()
        setupVoiceIndicatorView()
    }
    
    /// 设置 action bar view
    private func setupActionBar() {
        view.addSubview(chatActionBarView)
        chatActionBarView.snp.makeConstraints { [weak self] (make) in
            guard let self = self else { return }
            make.left.right.equalToSuperview()
            self.actionBarPaddingBottomConstranit = make.bottom.equalTo(-UIScreen.bottomSafeHeight).constraint
            make.height.equalTo(kChatActionBarOriginalHeight)
        }
        chatActionBarView.isHidden = true
    }
    
    private func setupMsgTableView() {
        view.addSubview(msgTableView)
        msgTableView.snp.makeConstraints { [weak self] (make) in
            guard let self = self else { return }
            make.left.right.equalToSuperview()
            make.top.equalTo(self.countdownTimeView.snp.bottom)
            make.bottom.equalTo(self.chatActionBarView.snp.top)
        }
        
        if viewModel.inquiryInfo.type == .online {
            setupTableHeader()
        }
    }
    
    private func setupCountdownTimeView() {
        view.addSubview(countdownTimeView)
        countdownTimeView.snp.makeConstraints { (make) in
            make.top.equalTo(self.view.safeAreaLayoutGuide)
            make.left.right.equalToSuperview()
            make.height.equalTo(40)
        }
        countdownTimeView.countDownEndHandler = { [weak self] in self?.loadData() }
    }
    
    private func setupChatMoreView() {
        view.addSubview(chatMoreView)
        chatMoreView.snp.makeConstraints { [weak self] (make) in
            guard let self = self else { return }
            make.left.right.equalToSuperview()
            make.top.equalTo(self.chatActionBarView.snp.bottom).offset(UIScreen.bottomSafeHeight)
            make.height.equalTo(kMoreKeyboardHeight)
        }
    }
    
    private func setupVoiceIndicatorView() {
        view.addSubview(voiceIndicatorView)
        voiceIndicatorView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalToSuperview().offset(100)
            make.bottom.equalToSuperview().offset(-100)
        }
        voiceIndicatorView.isHidden = true
    }
    
    private func setupTableHeader() {
        msgTableView.tableHeaderView = tableHeaderView
        tableHeaderView.snp.makeConstraints { (make) in
            make.left.top.equalTo(0)
            make.width.equalTo(UIScreen.screenWidth)
            make.height.equalTo(kChatInquiryInfoHeaderDefaultHeight)
        }
        tableHeaderView.isHidden = true
        
        tableHeaderView.expandHandler = { [weak self] isExpand in
            guard let self = self else { return }
            self.viewModel.isTableHeaderExpand = isExpand
            self.updateTableHeaderViewLayout()
            self.msgTableView.reloadData()
        }
    }
    
    func setRefreshHeader() {
        if viewModel.hasMore {
            if msgTableView.se_header == nil {
                let header = SERefreshHeader { [weak self] in self?.pullToLoadMore() }
                header.stateLabel?.isHidden = true
                header.lastUpdatedTimeLabel?.isHidden = true
                msgTableView.se_header = header
            }
        } else {
            if msgTableView.se_header != nil {
                msgTableView.se_header = nil
            }
        }
    }
    
    func updateTableHeaderViewLayout() {
        tableHeaderView.snp.updateConstraints { (make) in
            make.height.equalTo(self.viewModel.tableHeaderViewHeight)
        }
        UIView.animate(withDuration: 0.25, delay: 0.0, options: .curveLinear) {
            self.view.layoutIfNeeded()
        } completion: { _ in
            self.tableHeaderView.relayoutUI(self.viewModel)
        }
    }
    
    func updateAllUIAfterLoadData() {
        self.msgTableView.isHidden = false
        
        if viewModel.isCountDown {
            countdownTimeView.fireTimer(with: viewModel)
        }
        
        self.chatActionBarView.isHidden = self.viewModel.chatActionBarHidden
        self.view.backgroundColor = self.viewModel.chatActionBarHidden ? .background : .white
        self.setRefreshHeader()
        if viewModel.inquiryInfo.type == .online {
            self.tableHeaderView.update(with: self.viewModel)
            self.updateTableHeaderViewLayout()
        } else {
            if viewModel.inquiryInfo.status == .finish {
                SEAlert.show(in: self, title: "本次导诊已结束", confirmTitle: "我知道了", singleAction: true)
            }
        }
        self.msgTableView.reloadData {
            self.msgTableView.scrollBottomToLastRow()
        }
    }
}
